<?php

/**
 * Aurorum - Open source digital signage
 *
 * @link http://digitalsignage.tk
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Aurorum
 * @package Aurorum
 */

/**
 * This class provides access to data layer.
 *
 * @package Aurorum
 * @subpackage Models
 */

class ADS_SlideItemModel extends ADS_DefaultModel {
    
    public function countItems($slideId) {
        if(!is_int($slideId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        return dibi::fetch('
          SELECT
            COUNT(*) as cnt
          FROM
            [:wp:ads_slide_items] AS t1
          INNER JOIN
            [:wp:ads_dataframes] AS t2 ON t1.frameId = t2.frameId
          WHERE
            t1.slideId = %i', $slideId, '
            %if', (bool)$this->useFilter, 'AND %and', $this->filter, '%end', '
          ORDER BY
            t1.itemId
        ')->cnt;
    }
    
    public function getItems($slideId) {
        if(!is_int($slideId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        return dibi::fetchAll('
          SELECT
            t1.*,
            t2.*
          FROM
            [:wp:ads_slide_items] AS t1
          INNER JOIN
            [:wp:ads_dataframes] AS t2 ON t1.frameId = t2.frameId
          WHERE
            t1.slideId = %i', $slideId, 
          '%if', (bool)$this->useFilter, 'AND %and', $this->filter, '%end',
          '%if', (bool)$this->useOrderBy, 'ORDER BY %by', $this->orderBy, '%end',
          '%if', (bool)$this->useLimit, 'LIMIT %i OFFSET %i', $this->limit, $this->offset
        );
    }
    
    public function getItem($itemId) {
        if(!is_int($itemId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        return dibi::fetch('
          SELECT
            t1.*,
            t2.*
          FROM
            [:wp:ads_slide_items] AS t1
          INNER JOIN
            [:wp:ads_dataframes] AS t2 ON t1.frameId = t2.frameId
          WHERE
            t1.itemId = %i', $itemId
        );
    }
    
    public function isZoneOccupied($slideId, $zoneId) {
        if(!is_int($slideId) || !is_string($zoneId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        return dibi::fetch('
          SELECT
            t1.*
          FROM
            [:wp:ads_slide_items] AS t1
          WHERE
            t1.slideId = %i AND t1.zoneId = %s', $slideId, $zoneId
        )?true:false;
    }
    
    public function addItem($item){
        if(!is_array($item)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        //insert
        dibi::query('INSERT INTO [:wp:ads_slide_items]', $item);
    }
    
    public function removeItem($itemId){        
        if(!is_int($itemId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        //remove item
        dibi::query('DELETE FROM [:wp:ads_slide_items] WHERE itemId = %i LIMIT 1', $itemId);
    }
    
    public function editItem($itemId, $item){        
        if(!is_int($itemId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        dibi::query('UPDATE [:wp:ads_slide_items] SET', $item, 'WHERE itemId = %i', $itemId);
    }

}
